//
//  ZZTaskDetail.swift
//  QMXK
//
//  Created by jenkin on 2018/12/8.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskDetailVC: ZZBaseVC {
    var dataDict:Dictionary<String, Any>?
    var data:Array<ZZTaskModel>! = []
    var dataList:ZZTaskListModel?
    var mainScrollView:ZZTaskScrollView?
    var showTextLabel:UILabel?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
        self.showTextLabel = ZZViewUtil.shareViewUtil.createLabel(textAlign: NSTextAlignment.center, text: "", font: CommenConst.shareCommentConst.KNFONT(15), color: ColorConst.shareColorConst.titleColor(), autoLine: true)
        self.view.addSubview(self.showTextLabel!)
        self.showTextLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(self.navH + 10)
            make.centerX.equalTo(self.view)
            make.width.equalTo(300)
        })
        
        NotificationCenter.default.addObserver(self, selector: #selector(showText(note:)), name: NSNotification.Name(rawValue: "dataShow"), object: nil)
    }
    
    @objc func showText(note: Notification){
        
        self.showTextLabel?.text = note.userInfo!["text"] as! String
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "dataShow"), object: nil)
    }
    
    override func setUI() {
        super.setUI()
        
        let titleLabe:UILabel = ZZViewUtil.shareViewUtil.createLabel(textAlign: NSTextAlignment.center, text: self.dataDict?["name"] as! String, font: CommenConst.shareCommentConst.KNFONT(15), color: UIColor.black, autoLine: false);
        self.view.addSubview(titleLabe);
        titleLabe.isUserInteractionEnabled = false
        titleLabe.frame = CGRect.init(x: 0, y: self.navH - 30, width: self.screenW, height: 30)
        
        let btn:UIButton = UIButton.init(frame: CGRect.init(x: self.screenW - 50, y: self.navH - 30, width: 30, height: 30))
        btn.setTitle("+", for: UIControl.State.normal)
        btn.setTitle("+", for: UIControl.State.selected)
        btn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        btn.addTarget(self, action: #selector(addAction(sender:)), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btn)
        
        let editbtn:UIButton = UIButton.init(frame: CGRect.init(x: self.screenW - 80 - 100, y: self.navH - 30, width: 100, height: 30))
        editbtn.addTarget(self, action: #selector(editAction(sender:)), for: UIControl.Event.touchUpInside)
        editbtn.setTitle("edit", for: UIControl.State.normal)
        editbtn.setTitle("edit", for: UIControl.State.selected)
        editbtn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        self.view.addSubview(editbtn)
        
        
        self.dataList = ZZTaskListModel.deserialize(from: self.dataDict)
        
        self.mainScrollView = ZZTaskScrollView.init(frame: CGRect.init(x: 0, y: self.screenH / 4, width: self.screenW, height: self.screenH / 2))
        self.mainScrollView?.backgroundColor = UIColor.orange
        self.view.addSubview(self.mainScrollView!)
        self.mainScrollView?.dataList =  self.dataList
        self.mainScrollView?.setUI()
        
        
        let btnW:CGFloat = self.screenW / 4;
        let topbtn:UIButton = UIButton.init(frame: CGRect.init(x: btnW * 0, y: self.screenH - 50, width: btnW, height: 30))
        topbtn.addTarget(self, action: #selector(topAction(sender:)), for: UIControl.Event.touchUpInside)
        topbtn.setTitle("高点画线", for: UIControl.State.normal)
        topbtn.setTitle("高点画线", for: UIControl.State.selected)
        topbtn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        self.view.addSubview(topbtn)
        
        
        let robtn:UIButton = UIButton.init(frame: CGRect.init(x: btnW * 1, y: self.screenH - 50, width: btnW, height: 30))
        robtn.addTarget(self, action: #selector(rotAction(sender:)), for: UIControl.Event.touchUpInside)
        robtn.setTitle("旋转", for: UIControl.State.normal)
        robtn.setTitle("高点画线", for: UIControl.State.selected)
        robtn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        self.view.addSubview(robtn)
        
        let lowbtn:UIButton = UIButton.init(frame: CGRect.init(x: btnW * 2, y: self.screenH - 50, width: btnW, height: 30))
        lowbtn.addTarget(self, action: #selector(lowAction(sender:)), for: UIControl.Event.touchUpInside)
        lowbtn.setTitle("低点画线", for: UIControl.State.normal)
        lowbtn.setTitle("低点画线", for: UIControl.State.selected)
        lowbtn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        self.view.addSubview(lowbtn)
        
        let backBtn:UIButton = UIButton.init(frame: CGRect.init(x: btnW * 3, y: self.screenH - 50, width: btnW, height: 30))
        backBtn.addTarget(self, action: #selector(lineBackAction(sender:)), for: UIControl.Event.touchUpInside)
        backBtn.setTitle("撤销", for: UIControl.State.normal)
        backBtn.setTitle("撤销", for: UIControl.State.selected)
        backBtn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        self.view.addSubview(backBtn)
    }
    
    @objc func topAction(sender: UIButton){
        self.mainScrollView?.drawLine(type: 0)
    }
    
    @objc func lowAction(sender: UIButton){
        self.mainScrollView?.drawLine(type: 1)
    }
    
    @objc func rotAction(sender: UIButton){
        
        print("旋转")
        //self.mainScrollView = ZZTaskScrollView.init(frame: CGRect.init(x: 0, y: self.screenH / 4, width: self.screenW, height: self.screenH / 2))
        self.mainScrollView?.frame =  CGRect.init(x: 0, y: self.navH + 30, width: self.screenW, height: self.screenH - self.navH - 30 - 30)
        self.mainScrollView?.layer.transform = CATransform3DMakeRotation(CGFloat(M_PI_2), 0, 0, 1)
    }
    //lineBackAction //取消画线
    @objc func lineBackAction(sender: UIButton){
        self.mainScrollView?.cancelLastLine()
    }
    
    @objc func editAction(sender: UIButton){
        let action:UIAlertController = UIAlertController.init(title: "修改基本配置", message: "", preferredStyle: UIAlertController.Style.actionSheet)
        weak var weakSelf = self
        let maxminAction:UIAlertAction = UIAlertAction.init(title: "最大最小值修改", style: UIAlertAction.Style.default) { (action) in
            weakSelf!.navigationController?.pushViewController(weakSelf!.maxminVC, animated: true)
        }
        
        
        let dataEditAction:UIAlertAction = UIAlertAction.init(title: "数据处理", style: UIAlertAction.Style.default) { (action) in
            
            let editDataVC:ZZTaskDataEditVC = ZZTaskDataEditVC.init(tableStyle: UITableView.Style.plain, tableFrame: weakSelf!.view!.bounds)
            editDataVC.dataDict = weakSelf?.dataDict
            weakSelf?.navigationController?.pushViewController(editDataVC, animated: true)
            
        }
        
        let cancelAction:UIAlertAction = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.destructive) { (action) in
            
        }
        
        action.addAction(maxminAction)
        action.addAction(dataEditAction)
        action.addAction(cancelAction)
        self.present(action, animated: false, completion: nil)
    }
    
    @objc func addAction(sender: UIButton){
        self.navigationController?.pushViewController(self.dataAddVC, animated: true)
    }
    
    lazy var maxminVC:ZZTaskMaxMinEditVC! = {
        let maxminVC:ZZTaskMaxMinEditVC = ZZTaskMaxMinEditVC.init()
        weak var weakSelf = self
        maxminVC.saveBlock =  { (data:Dictionary) in
            weakSelf?.dataDict?["allMax"] = data["allMax"]
            weakSelf?.dataDict?["allMin"] = data["allMin"]
            weakSelf?.dataList?.allMax = data["allMax"] as! CGFloat
            weakSelf?.dataList?.allMin = data["allMin"] as! CGFloat
            ZZBaseUtil.shareUtil.saveTypeName(data: weakSelf?.dataDict)
            weakSelf?.mainScrollView?.dataList =  self.dataList
            weakSelf?.mainScrollView?.setUI()
        }
        return maxminVC
    }()
    
    lazy var dataAddVC:ZZTaskDataAddVC = {
        var dataVC:ZZTaskDataAddVC = ZZTaskDataAddVC.init()
        dataVC.dataSaveBlock =  self.saveDataBlock
        return dataVC
    }()
    
    lazy var saveDataBlock:ClickSaveBlock = {
        weak var weakSelf = self
        let saveDataBlock:ClickSaveBlock = {(data:Dictionary?) in
            let model:ZZTaskModel? = ZZTaskModel.deserialize(from: data)
            if weakSelf?.dataList?.data == nil{
                weakSelf?.dataList!.data = Array.init()
            }
            
            weakSelf?.dataList?.data.append(model!)
            
            var dataList:Array? = weakSelf?.dataDict?["data"] as? Array<AnyObject>
            if dataList == nil{
                dataList = Array.init()
            }
            dataList?.append(data as AnyObject)
            weakSelf?.dataDict?["data"] = dataList
            ZZBaseUtil.shareUtil.saveTypeName(data:  weakSelf?.dataDict)
            weakSelf?.mainScrollView?.setUI()
        }
        return saveDataBlock
    }()
}
